<!-- 积分审批列表
 * @Descripttion: 
 * @Author: fubinbin binbin@ssc-hn.com
 * @Date: 2025-03-13
 * @LastEditors: fubinbin binbin@ssc-hn.com
 * @LastEditTime: 2025-03-13
-->
<template>
  <div class="ssc-list org-position-list">
    <div class="ssc-table">
      <div class="ssc-table-header">
        <span class="ssc-table-header-title">积分审批</span>
      </div>
      <a-tabs v-model:activeKey="activeKey" @change="handleChange">
        <a-tab-pane :key="item.value" :tab="item.label" v-for="item in typeList" />
      </a-tabs>
      <a-table :columns="columns" :data-source="dataList" :loading="loading" :pagination="pagination" @change="onPaginationChange">
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <span>
              <a @click="detail(record)">详情</a>
            </span>
          </template>
          <template v-if="column.dataIndex === 'auditType'">
            <span>
              {{
                typeList
                  .filter((v) => v.value === record.auditType)
                  .map((item) => item.label)
                  .join('')
              }}
            </span>
          </template>
          <template v-if="column.dataIndex === 'auditStatus'">
            <span>
              {{
                taskList
                  .filter((v) => v.value == searchParam.auditStatus)
                  .map((item) => item.label)
                  .join('')
              }}
            </span>
          </template>
          <template v-if="column.dataIndex === 'createTime'">
            {{ record.createTime ? formatToDateTime(record.createTime) : '' }}
          </template>
        </template>
      </a-table>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { onMounted, reactive, ref, watch } from 'vue'
  import { useRoute, useRouter } from 'vue-router'

  import { requestGetIntegralIssuerList } from '/@/api/integral/integralApprove'
  import { useQueryPagination } from '/@/hooks/useQueryPagination'
  import { formatToDateTime } from '/@/utils/date'

  const router = useRouter()
  const route = useRoute()
  const routePath = route.path

  /** 搜索条件 */
  const searchParam = reactive({
    auditStatus: 2,
  })
  const activeKey = ref(1)

  const typeList = ref([
    {
      value: 1,
      label: '积分发行',
    },
    {
      value: 2,
      label: '积分增发',
    },
    {
      value: 3,
      label: '积分销毁',
    },
  ])

  const taskList = ref([
    {
      value: 1,
      label: '已办',
    },
    {
      value: 2,
      label: '待办',
    },
  ])

  const stateList = ref([
    {
      value: 0,
      label: '待提交',
    },
    {
      value: 1,
      label: '待审核',
    },
    {
      value: 2,
      label: '已发行',
    },
    {
      value: 3,
      label: '驳回',
    },
    {
      value: 4,
      label: '撤销',
    },
  ])

  /** 搜索条件 */
  const formatParam = reactive({
    auditStatus: 2,
    auditType: 1,
  })

  const setFormat = (value) => {
    formatParam.auditStatus = value.auditStatus
    formatParam.auditType = activeKey.value
  }

  watch(
    () => searchParam,
    (value) => {
      if (value) setFormat(value)
    },
    { immediate: true, deep: true },
  )

  /** 表格列配置 */
  const columns = [
    { title: '编号', dataIndex: 'taskId' },
    { title: '积分名称', dataIndex: 'coinName' },
    { title: '积分代号', dataIndex: 'coinSymbol' },
    { title: '发起时间', dataIndex: 'createTime' },
    { title: '发起机构', dataIndex: 'companyName' },
    { title: '审批类型', dataIndex: 'auditType' },
    { title: '状态', dataIndex: 'auditStatus' },
    { title: '操作', key: 'action' },
  ]

  const { loading, queryList, dataList, pagination, onPaginationChange } = useQueryPagination<GetIntegralApproveListTypes.IRequest, GetIntegralApproveListTypes.IResponse[]>(formatParam, requestGetIntegralIssuerList)

  onMounted(() => {
    // 待办审批
    if (routePath === '/regulator/wait-approve') {
      searchParam.auditStatus = 2
      // 已办审批
    } else if (routePath === '/regulator/already-approve') {
      searchParam.auditStatus = 1
    }
    queryList()
  })

  /**
   * 重置
   */
  const clear = () => {
    searchParam.auditStatus = 2
    activeKey.value = 1
    setFormat(searchParam)
    search()
  }

  const handleChange = () => {
    setFormat(searchParam)
    search()
  }

  /**
   * 搜索
   */
  const search = async () => queryList({ current: 1 })

  /**
   * 详情
   * @param record 发行商
   */
  const detail = (record) => {
    router.push({
      path: '/integral-issuer/manage/detail',
      query: { id: record.auditId, type: 2, taskId: record.taskId, auditId: record.auditId, taskStatus: searchParam.auditStatus },
    })
  }
</script>

<style lang="less" scoped></style>
